package com.tmxk.api.days03.lf;

import com.tmxk.api.days02.lf.User;

import java.util.*;

/**
 *注册功能
 *      用户名(账号)
 *      密码(限制密码长度不少于6位)
 *      确认密码(校验第一次输入的密码和第二次输入的密码一致)
 *      手机号(手机号必须是数字,长度11位)
 *      邮箱(邮箱必须包含.@)
 *      身份证号(必须是18位
 *
 */
public class StringRegisterDemo {
    //创建Scanner键盘输入对象
    static Scanner sc = new Scanner(System.in);
    protected static Map<String,User> userMap = new HashMap<>();
    static {
        // 初始加载两条用户信息
        User user1 = new User("1001","Tom","123456");
        User user2 = new User("1002","Emma","000000");
        userMap.put(user1.getUserId(),user1);
        userMap.put(user2.getUserId(),user2);
    }
    public void userLogin(){
        System.out.println("===========用户登录=========");
        System.out.println("欢迎来到**平台");
        System.out.println("请输入用户姓名");
        String userName = sc.next();
        System.out.println("请输入密码");
        String password = sc.next();
        // 判断当前用户输入的 用户名和密码是否存在平台系统中
        Collection<User> users = userMap.values();
        boolean isBool = false;
        for (User u : users) {
            if (userName.equalsIgnoreCase(u.getUserName()) && password.equals(u.getPassword())) {
                isBool = true;
                break;
            }
        }
        if(isBool){
            System.out.println("登录成功,欢迎"+ userName +"登录");
            String mes = "登录成功,欢迎".concat(userName).concat("登录");
            System.out.println("mes:" + mes);
        }else {
            System.out.println("登录失败,密码或用户名有误");
        }
    }

    public void userRegister(){
        System.out.println("===========用户注册=========");
        System.out.println("请输入用户名:");
        String registerName = sc.next();
        System.out.println("请输入用户密码:");
        String pwd = sc.next();
        //校验密码长度
        while (pwd.length() < 6){
            System.out.println("注册用户密码不能少于6位,请重新输入用户密码:");
            pwd = sc.next();
        }
        System.out.println("请再输入密码:");
        String confirmPwd = sc.next();
        while (!confirmPwd.equals(pwd)){
            System.out.println("两次密码不一致,请重新输入密码:");
            confirmPwd = sc.next();
        }
        System.out.println("请输入手机号码:");
        String phone = sc.next();
        //定义手机号正则匹配
        String phoneReg = "1[0-9]{10}";
//        while(phone.length() != 11){
//            System.out.println("手机号必须是是11位,请重新输入:");
//            phone = sc.next();
//        }
        while (!phone.matches(phoneReg)){
            System.out.println("手机号必须是11位,请重新输入");
            phone = sc.next();
        }
        System.out.println("请输入邮箱:");
        String email = sc.next();
        while(email.indexOf("@") == -1 || email.indexOf(".") == -1){
            System.out.println("邮箱必须包含@和.,请重新输入:");
            email = sc.next();
        }
        System.out.println("请输入身份证号码:");
        String certificateNo = sc.next();
        String certificateNoReg = "([1-9][0-9]{17}|[1-9][0-9]{16}X)";
        while(!certificateNo.matches(certificateNoReg)){
            System.out.println("身份证号输入不合法,请重新输入:");
            certificateNo = sc.next();
        }
        System.out.println("用户注册成功");


        //将注册成功的用户信息添加到集合中
        User register = new User();
        //UUID 国际通用标识符号
        UUID uuid = UUID.randomUUID();
        register.setUserId(uuid.toString().replace("-",""));
        register.setUserName(registerName);
        register.setPassword(confirmPwd);
        register.setPhone(phone);
        register.setEmail(email);
        register.setCertificateNo(certificateNo);
        // 将注册信息存储在Map集合中
        userMap.put(register.getUserId(),register);
    }

    public void initUser(){
        System.out.println("用户名\t用户密码\t手机号\t邮箱\t身份证号\t用户id\t\t");
            for (User u : userMap.values()){
                System.out.println(u.getUserName() + "\t\t" + u.getPassword() + "\t\t" + u.getPhone() + "\t\t" + u.getEmail() + "\t\t" + u.getCertificateNo() + "\t\t" + u.getUserId());
            }
    }
    public static void main(String[] args) {
        StringRegisterDemo registerDemo = new StringRegisterDemo();
        System.out.println("欢迎来到**平台");
        int chose = 0;
        do {
            //调用  系统平台中初始成员信息
            registerDemo.initUser();
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
            try {
                System.out.println("\n请选择 1.登录    2.注册");
                chose = sc.nextInt();
                switch (chose) {
                    case 1:
                        registerDemo.userLogin();
                        break;
                    case 2:
                        registerDemo.userRegister();
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                System.out.println("程序执行完毕!");
            }
        }while(chose == 1 || chose == 2);






    }
}
